<?php
session_start();
?>
<html>
<?php
    
    if (isset($_SESSION['LAST_ACTIVITY']) && (time() - $_SESSION['LAST_ACTIVITY'] > 1800)) {
    // last request was more than 30 minates ago
    session_unset();     // unset $_SESSION variable for the runtime 
    session_destroy();   // destroy session data in storage
    
    $host  = $_SERVER['HTTP_HOST'];
    $uri   = rtrim(dirname($_SERVER['PHP_SELF']), '/\\');
    $extra = 'index.php';
    header("Location: http://$host$uri/$extra?expire=true");
    exit;
}
$_SESSION['LAST_ACTIVITY'] = time(); // update last activity time stamp
?>    
    <head>

        <meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
        <title>Botany Dichotomous Key Conversion System</title>
        <link rel="stylesheet" type="text/css" href="css/default.css" />
        <link rel="stylesheet" type="text/css" href="css/stepsMenu.css" />
        
        <script src="javascripts/imageCheck.js"></script>
       
    </head>
        
<body>
    <script type="text/javascript" src="javascripts/jquery.js"></script>
    <script type="text/javascript" src="javascripts/addimage.js"></script>
    <?php
        
                $uploaddir= $_POST["output"];
                $inputFile = $uploaddir."keyconversion.xml";
                //echo "The file is ". $inputFile;
                $xml = simplexml_load_file($inputFile);
                $sxe = new SimpleXMLElement($xml->asXML());

                $basicInfoNode = $sxe->splash[0];
                
                /***************/
                if($basicInfoNode->count() > 1){
                    unset($basicInfoNode->key_title);
                    unset($basicInfoNode->key_author);
                    unset($basicInfoNode->date);
                    unset($basicInfoNode->bg_image);
                }
                /********************/
                $basicInfoNode->addChild("key_title", $_POST['basicInfoTitle']);

                $basicInfoNode->addChild("key_author", $_POST['basicInfoAuthor']);
                
                $basicInfoNode->addChild("date", $_POST['basicInfoDate']);
                
                $imgFileTmpLoc = $_FILES["basicInfoImage"]["tmp_name"];
               // echo "file temp location:".$imgFileTmpLoc."<br />";
                //echo "image file name = ".$_FILES["basicInfoImage"]["name"]."<br />";
                $imgFileName = "basicinfo.png";
                if(!empty($_FILES["basicInfoImage"]["tmp_name"])){
                        //$imgFileTmpLoc = $_FILES["basicInfoImage"]["tmp_name"]; // File in the PHP tmp folder
                        $splitName = explode(".", $_FILES["basicInfoImage"]["name"]); // Split file name into an array using the dot
                    $imgFileExt = end($splitName); // Now target the last array element to get the file extension
                    
                    //Check if uploaded image is in .png format, if not then convert
                        if (strtolower($imgFileExt) != "png") {
                            //echo "Uploaded image is not in .PNG format <br />";
                            //echo "file extension:". $imgFileExt."<br />";
                            //echo "target file: ".$imgFileTmpLoc."<br />";
                            $new_png = $uploaddir .$imgFileName;
                            //echo "new png img: ".$new_png."<br />";
                            $ext = strtolower($imgFileExt);
                            $new_img = "";
                            if ($ext == "gif"){ 
                                $new_img = imagecreatefromgif($imgFileTmpLoc);
                            } else if($ext =="jpg"){ 
                                $new_img = imagecreatefromjpeg($imgFileTmpLoc);
                            }
                            imagepng($new_img, $new_png);
                            $basicInfoNode->addChild("bg_image", "$imgFileName");
                            imagedestroy($imgFileTmpLoc);
                         }
                        else{
                            //echo "Uploaded image is in .PNG format <br />";
                            //Upload  image file
                            move_uploaded_file($_FILES["$glossaryImage"]["tmp_name"], $uploaddir.$imgFileName);
                            $basicInfoNode->addChild("bg_image", "$imgFileName");
                        }
                    
                }
                else {
                    $basicInfoNode->addChild("bg_image");     
                }
                

                // FOR PROPER FORMATTING XML
                    // Create a new DOMDocument object
                    $dom = new DOMDocument('1.0');
                    // add spaces, new lines and make the XML more readable format        
                    $dom->preserveWhiteSpace = false;
                    $dom->formatOutput = true;
                    //Load XML document from a string
                    $dom->loadXML($sxe->asXML());
                    //Save XML to file 
                    $dom->save($inputFile);
    
    ?>
        
    <?php include("includes/header.php"); ?>
     <div id="page-wrap2">
                <div id="main-content">

                    <div id="left-col">
                        <?php include("includes/menuAddimages.php"); ?>
                    </div><!-- end of left-col -->
                <div id = "right-col">
                    <h3>Step 3: Upload Organism Images</h3>
                    <form action="AddNodesImages.php" method="post" enctype="multipart/form-data">
                        <fieldset id="imageField">
                            <p class="instruction">Please select only .PNG, .JPG and .GIF image files to upload using <i>"Add Image"</i> link. 
                                You can upload multiple images for each organism. Uploading images for the organisms is optional.</p>
                            <?php
                                #$filename = $_POST["filename"];
                                #echo "$filename";
                                $inputFile = $uploaddir."keyconversion.xml";
                                $xml = simplexml_load_file($inputFile);
                                //echo $xml->getName() . "<br />";
                                //foreach($xml->children() as $child){
                                  //  echo $child->getName() . ": " .$child. "<br />";
                                //}

                                //Organisms and their image upload 
                                $Orgcount = count($xml->organisms->organism);
                                //echo "Organism count = ".$Orgcount."<br />";
                                echo "<div ID=\"organisms\">";
                                echo "<h4><i> Organisms </i></h4>";
                                echo"<table width=\"100%\" cellspacing=\"10\"><tr><td valign=\"top\">";
                                echo"<table class='orgTable'>";
                                for($i = 0; $i < $Orgcount; $i++){
                                    
                                    $orgButtonId = "org".$xml->organisms->organism[$i]->org_id."image";
                                    $orgDivId = $orgButtonId."div";
                                    
                                        echo "<tr><td>";
                                        echo "<br />".$xml->organisms->organism[$i]->org_id. ". ";
                                        echo $xml->organisms->organism[$i]->common_name."<br />";
                                        echo "<a id=\"$orgButtonId\" href=\"#\">Add Image (1 or more)</a>";
                                        echo "<br/>";
                                        echo "</td></tr>";
                                        
                                    if($Orgcount % 2 == 0 )
                                    {
                                        if(($i +1) == $Orgcount/2)
                                        {
                                            echo"</table></td><td valign=\"top\">";
                                            echo"<table class='orgTable'>";
                                        }
                                    }
                                    else 
                                    {
                                        if(($i + 1) == ($Orgcount+1)/2)
                                        {
                                            echo"</table></td><td valign=\"top\">";
                                            echo"<table class='orgTable'>";
                                        }                                        
                                    }

                                    #echo $xml->organisms->organism[$i]->org_id. "&nbsp";
                                    #$orgButtonId = "Org".$xml->organisms->organism[$i]->org_id."Image";
                                    #$orgDivId = $orgButtonId."div";
                                    #echo $xml->organisms->organism[$i]->common_name."<br />"; 
                                    #echo "<a id=\"$orgButtonId\" href=\"#\">Add Image</a>";
                                    #echo "<br><br>  ";
                                }//end of for
                                echo"</table>";
                                echo"</td></tr></table>";
                                echo "</div>";                                
                            ?>
                        
                            <div style="text-align:center;">
                           <input type="hidden" name="output" value=<?php echo $uploaddir ?> /> <br/><br/>   
                            <input type="submit" class="next" value="Next"/> <br /><br />
                          </div>
                            
                        </fieldset>

                    </form>
                </div><!-- end of right-col -->
                <div class="clear"></div> <!-- all the content inside main-content div element is floated, need to clear float...
                                              because height of main-content does not know how tall it would be -->

            </div><!-- end of main-content -->        
        </div>  <!-- End of page-wrap -->                       
    <?php include("includes/footer.php"); ?>
</body>
</html> 

